<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>投票发起</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
</script>
<body style="padding: 10px">
<div style="display: none" id="bodys">
    <form action="" class="layui-form layui-form-pane" lay-filter="example">
        <h1 align="center" style="margin-bottom: 20px">投票</h1>
        <!--流程id-->
        <input name="pkVoteId" id="pkVoteId" type="hidden"/>
        <!--员工id-->
        <input name="personnelId" id="personnelId" type="hidden"/>


        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">投票标题</label>
                <div class="layui-input-block">
                    <input type="text" name="voteName" id="voteName" lay-verify="required" autocomplete="off" placeholder="请输入(必填)" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <div class="layui-input-block">
                <div id="voteTypeSuperior" class="xm-select-demo"></div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">详细内容</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" id="voteBody"
                      name="voteBody" lay-verify="required">
            </textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>投票选项</legend>
            <div id="voteOption">
                <div class="layui-form-mid layui-word-aux">至少需要两个投票选项，每项最多100个字</div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px">
                        <input type="text" name="voteOptionName" autocomplete="off" placeholder="请输入投票选项" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px">
                        <input type="text" name="voteOptionName" autocomplete="off" placeholder="请输入投票选项" class="layui-input">
                    </div>
                </div>
            </div>
            </fieldset>
            <input type="button" class="layui-btn layui-btn-normal" id="addVoteOption" value="新增选项 +"/>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">截至时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="vateDeadline" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">投票者</label>
            <div class="layui-input-block">
                <div id="demo1" class="xm-select-demo"></div>
            </div>
        </div>

        <!-- 提交-->
        <div class="layui-form-item" id="sub">
            <div class="layui-input-block" id="flows">
                <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">立即提交</button>
                <input type="button" class="layui-btn  layui-btn-primary" id="deleteById" value="取消"/>
            </div>
        </div>
    </form>

</div>
<script>
    let pkVoteId = getQueryString("pkVoteId");
    let pkVoteTypeId;
    let disabled = false;
    layui.use(['form', 'laydate', 'table','laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });

        $("#addVoteOption").on('click',function () {
            $("#voteOption").append('<div class="layui-form-item">\n' +
                '                    <div class="layui-input-block" style="margin-left: 0px">\n' +
                '                        <input type="text" name="voteOptionName" autocomplete="off" placeholder="请输入投票选项" class="layui-input">\n' +
                '                    </div>\n' +
                '                </div>')
        });

        let res;
        if (pkVoteId) {
            res = getAjaxById(getRootPath() + "OaVoteAction/findById/" + pkVoteId);
            pkVoteTypeId = res.VoteTypeId;
            form.render();
        }

        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath()+'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function(){
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            pkJurisdictionId = xmSelect.render({
                el: '#voteTypeSuperior',
                radio: true,
                disabled: disabled,
                name:'voteTypeId',
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [ -1 ],
                },
                height: 'auto',
                data: loadVoteType(pkVoteTypeId)
            });

            var demo1 = xmSelect.render({
                el: '#demo1',
                autoRow: true,
                filterable: true,
                name:'personnelId',
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [ -3 ],
                },
                toolbar: {
                    show: true,
                    list: ['ALL', 'REVERSE', 'CLEAR']
                },
                height: 'auto',
                data:loadPersonnel()
            })

        });

        function loadPersonnel(){
            let url = getRootPath()+"OaPersonnelAction/selectAll";
            let res = getAjaxById(url);

            if(res){
                //循环公司
                $.each(res,function (index,item) {
                    console.info();
                    //判断公司下面是否有部门
                    if(!$.isEmptyObject(item.children)){
                        //循环部门
                        $.each(item.children,function (indexs,items) {
                            if(!$.isEmptyObject(items.children)){
                                $.each(items.children,function (indexz,itemz) {
                                    if($.isEmptyObject(itemz.children)){
                                        items.children.splice(indexz,1);
                                    }
                                });
                            }else{
                                item.children.splice(indexs,1);
                            }
                        });
                    }else{
                        delete res[item];
                    }

                });
                return res;
            }else{
                return [];
            }
        }

        function loadVoteType(id){
            let url = getRootPath()+"OaVoteTypeAction/loadVoteType";
            let data = {
                id : id,
                show:"1"
            };
            let res = getAjax(url,data);
            if(res){
                return res;
            }else{
                return [];
            }
        }

        /*let data=getAjaxById(getRootPath()+"OaRegularFlowAction/isExist");
        if(data){
            //墨绿深蓝风
            layer.alert('你已提交转正申请请勿重复提交', {
                skin: 'layui-layer-molv' //样式类名
                ,closeBtn: 0
            }, function(){
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        }*/

        form.render();

        //监听起草提交
        form.on('submit(demo1)', function (data) {

            let ids = [];
            $("input[name=voteOptionName]").each(function (index,item) {
                if(item.value){
                    ids.push(item.value);
                }
            });
            if(ids.length < 1){
                layer.msg('请填写至少俩个投票选项', {icon: 2});
                return false;
            }
            if(!data.field.personnelId){
                layer.msg('请选择至少一个投票人', {icon: 2});
                return false;
            }

            if(!data.field.voteTypeId){
                layer.msg('请选择投票所属类型', {icon: 2});
                return false;
            }

            data.field.voteOptionName = ids;
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            const res = postAjax(getRootPath() + "OaVoteAction/add", data.field);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        $("#deleteById").on("click",function () {
            //询问框
            layer.confirm('是否取消投票', {
                btn: ['确认','取消'] //按钮
            }, function(){
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });

        });
        /**
         * 禁用
         *  @param forms 控件
         */
        function formDisabled(forms) {
            const DISABLED = 'layui-btn-disabled';
            forms.attr('disabled', 'disabled');
            forms.addClass(DISABLED);
        }


        $("#bodys").show();
        form.render();
    });
</script>
</body>
</html>